$(function(){
	checkALL();
	var munalForm = $('#manual-exam-form');	
	
	munalForm.on('change',saveManualForm);
	/**
	 * 获取问题列表  手动组卷
	 */
	$("#exam-manual-question").on('click','.itembank-question-num',function(){
		var container = $('#manual-questions-container');
		var itemTemplate = _.template($('#manual-questions-template-first').html());
		var itembankId = $(this).attr('data-id');
		$('#exam-manual-question').find('.itembank-question-num').removeClass('on');
		$(this).addClass('on');
		munalForm.find('.questions-add').removeAttr("disabled");
		$('.loading').show();
		var hasId = new Array();
		munalForm.find('input[name^=questions_]').each(function(k,v){
			var id = $(this).val();
			hasId[k] = parseInt(id);
		});
		container.ifEmpty(function(el){
            el.html('<div class="margin-t-10 clearfix options-group">题库中没有题目可供选择了，点击<a href="/biz/index">创建</a></div>');
        });
		var params = {
				url: '/biz/question/questions/'+itembankId,
				type: 'GET',
				dataType: 'json',
				data: {itembank_id:itembankId},
		};
		$.ajax(params).done(function(resp){
			$('.loading').hide();
			if(resp.status == 'success') {
				container.html(itemTemplate({questions:resp.data,hasid:hasId}));
				munalForm.find('input[name=itembank]').val(itembankId);
				inintIcheck();
				checkALL();
			}else{
				layer.open({title:'提示',content:resp.msg});
			}
		}).fail(function(erro){
			error(erro);
		});
	});
	$("#exam-manual-question").on('click','.questions-add',function(){
		var container = $('#manual-questions-preview-container'); 
		var previewTemplate = _.template($('#manual-questions-template').html());
		var disabled = $(this).attr("disabled");
		if(disabled){
			layer.msg('请选择其它题库继续添加...');
			return false;
		}
		var html = "";
		var itembank_id = munalForm.find('input[name=itembank]').val();
		var hasChecked = false;
		var count = 0;
		var question_count = parseInt(munalForm.find('.count_ques').text());
		munalForm.find('input[name^=selectques]').each(function(k,v){
			if($(this)[0].checked == true && parseInt($(this).val()) > 0 ) {
				var id = parseInt($(this).val());
				var type = $(this).attr('data-type');
				var questions = $(this).parent().parent().next().html();
				container.append(previewTemplate({id:id,questions:questions,type:type}));
				hasChecked = true;
				count +=1;
			}
		});
		container.append('<input type="hidden" name="itembank_id[]" value="'+itembank_id+'">');
		munalForm.find('.count_ques').text(question_count+count);
		//$(this).attr("disabled","disabled");
		layer.msg('添加成功，你可以点击试卷设置设定分值或选择其它题库继续添加...');
	});
	$('#exam-manual-question').on('click','.exam-to-show',function(){
		var preview = $('#manual-questions-preview-container');
		var itembanks = $('#exam-itembank-check');
		var questions = $('#manual-questions-container');
		var modalfooter = $('#munal-modal-footer');
		if(preview.hasClass('hide')){
			$(this).text('添加题目');
			preview.removeClass('hide');
			itembanks.addClass('hide');
			questions.addClass('hide');
			modalfooter.addClass('hide');
		} else {
			$(this).text('试卷设置');
			preview.addClass('hide');
			itembanks.removeClass('hide');
			questions.removeClass('hide');
			modalfooter.removeClass('hide');
		}
	});
	$('#exam-manual-question').on('click','.munal-destroy',function(){
		var question_count = parseInt(munalForm.find('.count_ques').text());
		var id = $(this).attr('data-id');
		var scoreCount = parseInt(munalForm.find('.count_score').text());
		var score = parseInt(munalForm.find('input[name=score_'+id+']').val());
		if(isNaN(score) || typeof(score) == 'undefined' || score == '') {
			score = 0;
		}
		$(this).parent().parent().remove();
		question_count -= 1;
		munalForm.find('.count_score').text(scoreCount-score)
		munalForm.find('.count_ques').text(question_count);
	});
	/**
	 * 手动组卷提交
	 */
	$('#exam-manual-question').on('click','.munal-exam-submit',function(){
		var data = munalForm.serializeArray();
		var disabled = $(this).attr("disabled");
		if(disabled){
			return false;
		}
		$(this).attr("disabled","disabled");
		var count_ques = munalForm.find('.count_ques').text();
		var count_score = munalForm.find('.count_score').text();
		if(isEmpty(count_ques)){
			$(this).removeAttr("disabled"); 
			layer.open({
				title:'错误消息',
				content:'请选择题目'
			});
			return false;
		} 
		if(isEmpty(count_score)){
			$(this).removeAttr("disabled"); 
			layer.open({
				title:'错误消息',
				content:'分值不能为空，请选择题目'
			});
			return false;
		} 
		var pass_prop = munalForm.find('input[name=pass_prop]').val();
		if(isEmpty(pass_prop)){
			$(this).removeAttr("disabled"); 
			layer.open({
				title:'错误消息',
				content:'合格比不能为空'
			});
			return false;
		}
		data.push( {
			name:"question_count",
			value:count_ques
		});
		data.push({
				name:"score_count",
				value:count_score
		});
		var params = {
				url: '/biz/exam/munal-create',
				type: 'POST',
				dataType: 'json',
				data:data,
		};
		var container = $('#company-exam-list');
		var listItemTemplate = _.template($('#items-template').html());
		$.ajax(params).done(function(data){
			if(data.status == 'success'){
				munalForm.get(0).reset();
				munalForm.find('.count_ques').text(0);
				munalForm.find('.count_score').text(0)
				$('#manual-questions-preview-container').html('');
				munalForm.find('.questions-add').removeAttr("disabled");
				munalForm.find('.munal-exam-submit').removeAttr("disabled"); 
				$('#exam-manual-question').modal('hide');
				$('.loading').hide();
				layer.msg(data.msg);
				container.prepend(listItemTemplate({item:data.data}));
			} else {
				munalForm.find('.munal-exam-submit').removeAttr("disabled"); 
				layer.open({title:'提示',content:data.msg});
			}
		}).fail(function(erro){
			munalForm.find('.munal-exam-submit').removeAttr("disabled"); 
			error(erro);
		});
		return false;
	});
	
	function saveManualForm(){
		var score_count = 0;
		munalForm.find('.count_score').text(0);
		
		munalForm.find('input[name^=score_]').each(function(key,value){
			if(parseInt($(this).val()) > 0){
				score_count += parseInt($(this).val());
			}
		});
		munalForm.find('.count_score').text(score_count);
	}
	function checkALL(){
		munalForm = $('#manual-exam-form');
		munalForm.find('input[name=checkAll]').on('ifChecked', function(event){
			munalForm.find('input:checkbox').iCheck('check');
		});
		munalForm.find('input[name=checkAll]').on('ifUnchecked', function(event){
			munalForm.find('input:checkbox').iCheck('uncheck');
		});
	}
});